/*******************************************************************************
 File Name        : fnetprotocol.h
 Summary          : A Net Protocol for ExtensionEX and pluginEX.with step pack
 Create           : 2023-08-24
 Author           : Schindleren
 Update history   :
 NO.     Author        Date         Content
 001     Schindleren   2023-08-24   Create New
*******************************************************************************/
#ifndef FNETPROTOCOL_H
#define FNETPROTOCOL_H

#include <QByteArray>

class FNetProtocol
{
    //网络包
    typedef struct NETDATA_PACK_STRU
    {
        quint8 iHeadA;			//包头A
        quint8 iHeadB;			//包头B
        qint32 s32Lenth;			//包长度
    } NETDATA_PACK;

public:
    explicit FNetProtocol();
    ~FNetProtocol();

    qint32 NetPackFill(const QByteArray &byteData,QByteArray &bytePack/*,BOOL bCompress = TRUE*/);

    qint32 NetUnPack(QByteArray &byteBuf);

    //清除缓存数据，用于端口重连。或者解包异常时清除
    bool ClearData();

    virtual qint32 RecvPackData(QByteArray &bytePack) = 0;
private:
    const quint8  u8PacketHeaderA;
    const quint8  u8PacketHeaderB;
    QByteArray  m_curPackArr;   //获取当前数据包缓冲数据
    bool m_bPackGotFlagA;       //是否获取到包头A
    bool m_bPackGotFlagB;       //是否获取到包头B
    qint32 m_s32CurPackLen;      //当前包解析出来长度
    qint32 m_s32CheckErr;
    // 计算校验和
    quint8 CheckSum(const QByteArray &bufBody);

    int DbgHex8(QByteArray &byteData);
};

#endif // FNETPROTOCOL_H
